<?php

namespace Admin\Controller;

class SeoController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        F('seo', null);
    }

    // 列表
    public function lists()
    {
        $map = '';
        $seo = D('Seo')->get_list($map, true, '', '');
        $this->assign('seo', $seo);
        $this->display();
    }

    // 批量保存
    public function allsave()
    {
        $id          = I('post.id');
        $title       = I('post.title');
        $keywords    = I('post.keywords');
        $description = I('post.description');
        $num         = 0;
        $data        = M("Seo");
        if (is_array($id) && count($id) > 0) {
            foreach ($id as $k => $v) {
                unset($map);
                $map['id']          = intval($v);
                $map['title']       = trim($title[$k]);
                $map['keywords']    = trim($keywords[$k]);
                $map['description'] = trim($description[$k]);
                $data->save($map);
                $num++;

            }
        }
        if ($num > 0) {
            $this->success('成功保存' . $num . '条信息！');
        } else {
            $this->error('未保存任何信息！');
        }
    }

    // 新增
    public function add()
    {
        if (IS_POST) {
            $data = D("Seo");
            if (!$data->create()) {
                $this->error($data->getError());
            } else {
                $data->add();
                $this->success('保存完成', 'lists');
            }
        } else {
            $this->display();
        }
    }

    // 编辑
    public function edit()
    {
        if (IS_POST) {
            $data = D("Seo");
            if (!$data->create()) {
                $this->error($data->getError());
            } else {
                $data->save();
                $this->success('保存完成', 'lists');
            }
        } else {
            $id = I('get.id', '', 'intval');
            if ($id) {
                $data      = M("Seo");
                $map['id'] = $id;
                $show      = $data->where($map)->find();
                $this->assign('show', $show);
                $this->display();
                //dump($show);
            } else {
                $this->error('非法请求');
            }
        }
    }

    // 删除
    public function delete()
    {

        $ids = I('request.id');
        $ids = is_array($ids) ? implode(",", $ids) : $ids;
        if ($ids && preg_match("/^(\d{1,10},)*(\d{1,10})$/", $ids)) {
            $data = M("Seo");
            $s    = $data->delete($ids);
            $this->success("成功删除{$s}条数据");
        } else {
            $this->error('请选择要删除的信息！');
        }

    }

}